<template>
  <Temp @clear="clear" @close="close">
    <div class="viewfield">
      <div
        @click="activeTool(item.title)"
        v-for="item in list"
        :key="item.title"
        :style="{
          color:
            active === item.title
              ? activeColor
              : defaultColor
        }"
      >
        <Icon
          :title="item.title"
          :name="item.icon"
          :font-size="iconSize"
        />
        <span>{{ item.title }}</span>
      </div>
    </div>
  </Temp>
</template>

<script>
import Temp from '../temp'
import Icon from '@/components/icon'
import { getAction } from '@/utils/helper'

const viewfield = {}
export default {
  components: {
    Temp,
    Icon
  },
  inject: ['viewerId'],
  data() {
    return {
      iconSize: 20,
      activeColor: 'rgba(0, 210, 255, 1)',
      defaultColor: '#D2E6F0',
      active: '',
      list: [
        {
          title: '添加视域',
          icon: 'add'
        }
      ]
    }
  },
  async created() {
    const { toolManager } = await getAction(this.viewerId)
    viewfield[this.viewerId] = toolManager.viewfield
  },
  methods: {
    close() {
      this.clear()
      this.$emit('close')
    },
    clear() {
      this.active = ''
      viewfield[this.viewerId].clear()
    },
    activeTool(title) {
      if (this.active === title) {
        this.clear()
      } else {
        this.clear()
        this.active = title
        switch (title) {
          case '添加视域':
            viewfield[this.viewerId].start()
            break
          default:
            break
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.viewfield {
  display: flex;
  align-items: center;

  & > div:last-child {
    margin-left: 10px;
  }

  & > div {
    display: flex;
    align-items: center;

    & > span {
      margin-left: 4px;
      font-size: 14px;
      word-break: keep-all;
      cursor: pointer;
    }
  }
}
</style>
